<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            @clear="getOrderList"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="OrderList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay === '0'"
              >未付款
            </el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-tooltip
              effect="dark"
              content="修改地址"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="editSiteDialogVisible = true"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="物流进度"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                icon="el-icon-location"
                size="mini"
                @click="showProgressBox"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editSiteDialogVisible"
      width="50%"
      @close="editSiteDialogClose"
    >
      <el-form ref="addressRef" :model="addressForm" :rules="addressRules">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="catyData"
            :props="{ expandTrigger: 'hover' }"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editSiteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSite">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import catyData from './citydata'
export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 返回的总条数
      total: 0,
      // 商品订单数据
      OrderList: [],
      // 修改地址对话框开关
      editSiteDialogVisible: false,
      // 修改地址表单双向绑定对象
      addressForm: {
        address1: [],
        address2: ''
      },
      // 表单校验对象
      addressRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      },
      // 省市区/县数据
      catyData,
      // 物流地址对话框开关
      progressDialogVisible: false,
      // 模拟物流数据
      activities: [
        {
          content: '已签收，欢迎再次使用顺丰快递',
          timestamp: '2018-04-15'
        },
        {
          content: '快件交给xxx,正在派送途中(联系电话:xxxxxxxxxx,顺丰已开启“安全呼叫”保护您的电话隐私,请放心接听!)(主单总件数:1件)',
          timestamp: '2018-04-16'
        },
        {
          content: '快件到达【广州天河长滋东路营业部】',
          timestamp: '2018-04-16'
        },
        {
          content: '快件在【广州新塘中转场】完成分拣,准备发往【广州天河长泣东路营业部】',
          timestamp: '2018-04-15'
        },
        {
          content: '快件已发车',
          timestamp: '2018-04-15'
        }, {
          content: '快件在【附城速运营业点】完成分拣,准备发往【河源南城集散点】',
          timestamp: '2018-04-13'
        }, {
          content: '顺丰速运已收取快件',
          timestamp: '2018-04-11'
        }]
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    async getOrderList () {
      console.log(this.queryInfo)
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品订单列表失败！')
      }
      this.OrderList = res.data.goods
      this.total = res.data.total
      this.$message.success('获取商品订单列表成功！')
    },
    // 监听每页显示数目改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听当前页数改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 确认修改地址
    editSite () {
      // 未提供修改地址接口，故未发起网络请求
      this.$refs.addressRef.validate(valid => {
        if (!valid) { return this.$message.error('表单校验未通过') }
        this.editSiteDialogVisible = false
      })
    },
    // 关闭修改地址对话框触发
    editSiteDialogClose () {
      this.$refs.addressRef.resetFields()
    },
    // 展示物流地址对话框
    showProgressBox () {
      // 接口已失效，使用本地数据activities模拟
      this.progressDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
